<?php


namespace addons\Minerva\api\modules\v1\controllers;

use addons\Minerva\common\models\MainConfig;
use addons\Minerva\common\models\Tasks;
use common\enums\StatusEnum;
use common\helpers\HashidsHelper;
use common\helpers\ResultHelper;
use common\helpers\StringHelper;
use common\traits\Curd;
use Ramsey\Uuid\UuidFactory;
use Yii;
use addons\Minerva\common\traits\FileActions;
use api\controllers\OnAuthController;
use yii\data\ActiveDataProvider;

/**
 * TaskController.php
 *
 * @Class TaskController
 * @Aackage addons\Minerva\api\modules\v1\controllers
 * @Author 小武哥<1240035958@qq.com>
 * @Date 2021/9/2 14:52
 * @Version 1.0
 */
class TaskController extends OnAuthController
{

    use Curd;

    /**
     * @var Tasks
     */
    public $modelClass = Tasks::class;

    protected $authOptional = ['images','videos','voices','files','merge'];

    public function actionIndex(){
        $get = Yii::$app->request->get();
        $list = new ActiveDataProvider([
            'query' => $this->modelClass::find()
                ->with(['backendMember','attachment'])
                ->where(['status' => $get['status']])
                ->andFilterWhere(['task_level' => $get['task_level']])
                ->andFilterWhere(['like','task_title',$get['task_title']])
                ->orderBy('created_at desc')
//                ->select('id,jiben_name,jiben_idcard,created_at,state')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
        $model = $list->getModels();
        foreach ($model as $key => $val){
            $user = [];
            $user[] = "ID：" . $val['backendMember']['id'];
            $user[] = "账号：" . $val['backendMember']['username'];
            $user[] = "姓名：" . $val['backendMember']['realname'];
            $model[$key]['user'] = $user;
            unset($model[$key]['backendMember']);
        }
        $list->setModels($model);
        return $list;
    }

    public function actionAdd(){
        $post = Yii::$app->request->post();
        $model = new $this->modelClass();
        $model->attributes = $post;
        $model->client_ip = Yii::$app->request->getUserIP();
        $model->client_type = 'web';
        $model->user_id = Yii::$app->user->identity->member_id;
        $uuid = new UuidFactory();
        $model->task_id = '' . strtoupper($uuid->uuid1());
        if (!$model->save()) {
            return ResultHelper::json(422, $this->getError($model));
        }
        return $model;
    }

    public function actionComplete($id){
        if (!($model = $this->modelClass::findOne($id))) {
            return ResultHelper::json(422, '找不到数据');
        }
        $config = MainConfig::find()
            ->asArray()
            ->one();
        if($config['check_model'] == 0){
            $model->status = 3;
        }else{
            $model->status = 2;
        }
            if ($model->save()) {
            return ResultHelper::json(200, '文件上传成功');
        }
        return ResultHelper::json(422, '保存失败，请稍后重试！');
    }

    /*
     * 任务状态
     * -2上传失败;-1伪造;0审核不通过;1上传中;2待审核(上传完成);3已审核;4病毒检测;5网间交互;6完成
     */
    public function actionTaskStatus($id){
        $status = Yii::$app->request->post('status');
        if (!($model = $this->modelClass::findOne($id))) {
            return ResultHelper::json(422, '找不到数据');
        }
//        switch ($status){
//            case 2:
//
//        }
        $model->status = $status;
        if ($model->save()) {
            return ResultHelper::json(200, '保存成功');
        }
        return ResultHelper::json(422, '操作失败，请稍后重试！');
    }

    /**
     * 伪删除(放入回收站)
     *
     * @param $id
     */
    public function actionDestroy($id)
    {
        if (!($model = $this->modelClass::findOne($id))) {
            return ResultHelper::json(422, '找不到数据');
        }
        $model->status = StatusEnum::DELETE;
        if ($model->save()) {
            return ResultHelper::json(200, '删除成功');
        }
        return ResultHelper::json(422, '删除失败');
    }

    /**
     * 删除（彻底删除）
     *
     * @param $id
     */
    public function actionDel($id)
    {
        if ($this->findModel($id)->delete()) {
            return ResultHelper::json(200, '删除成功');
        }
        return ResultHelper::json(422, '删除失败');
    }
}